package com.bangda.crm.service.impl;

import java.util.List;

import com.bangda.crm.domain.enums.OrderStatusEnum;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bangda.crm.mapper.CrmOrderMapper;
import com.bangda.crm.domain.CrmOrder;
import com.bangda.crm.service.ICrmOrderService;

/**
 * 客户订单Service业务层处理
 * 
 * @author fanboshi
 * @date 2023-10-17
 */
@Service
public class CrmOrderServiceImpl implements ICrmOrderService 
{
    @Autowired
    private CrmOrderMapper crmOrderMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询客户订单
     * 
     * @param orderId 客户订单主键
     * @return 客户订单
     */
    @Override
    public CrmOrder selectCrmOrderByOrderId(Long orderId)
    {
        return crmOrderMapper.selectCrmOrderByOrderId(orderId);
    }

    /**
     * 查询客户订单列表
     * 
     * @param crmOrder 客户订单
     * @return 客户订单
     */
    @Override
    public List<CrmOrder> selectCrmOrderList(CrmOrder crmOrder)
    {
        return crmOrderMapper.selectCrmOrderList(crmOrder);
    }

    /**
     * 新增客户订单
     * 
     * @param crmOrder 客户订单
     * @return 结果
     */
    @Override
    public int insertCrmOrder(CrmOrder crmOrder)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        if (StringUtils.isNotNull(sysUser.getNickName())){
            crmOrder.setCreateBy(sysUser.getNickName());
            crmOrder.setOrderPrincipal(sysUser.getNickName());
        }else {
            crmOrder.setCreateBy("该用户未设置名字");
        }

        crmOrder.setOrderStatus(OrderStatusEnum.UNFILLEDORDERS.getCode());
        crmOrder.setCreateTime(DateUtils.getNowDate());
        return crmOrderMapper.insertCrmOrder(crmOrder);
    }

    /**
     * 修改客户订单
     * 
     * @param crmOrder 客户订单
     * @return 结果
     */
    @Override
    public int updateCrmOrder(CrmOrder crmOrder)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        if (StringUtils.isNotNull(sysUser.getNickName())){
            crmOrder.setCreateBy(sysUser.getNickName());
            crmOrder.setOrderPrincipal(sysUser.getNickName());
        }else {
            crmOrder.setCreateBy("该用户未设置名字");
        }
        crmOrder.setUpdateTime(DateUtils.getNowDate());
        return crmOrderMapper.updateCrmOrder(crmOrder);
    }

    /**
     * 批量删除客户订单
     * 
     * @param orderIds 需要删除的客户订单主键
     * @return 结果
     */
    @Override
    public int deleteCrmOrderByOrderIds(Long[] orderIds)
    {
        return crmOrderMapper.deleteCrmOrderByOrderIds(orderIds);
    }

    /**
     * 删除客户订单信息
     * 
     * @param orderId 客户订单主键
     * @return 结果
     */
    @Override
    public int deleteCrmOrderByOrderId(Long orderId)
    {
        return crmOrderMapper.deleteCrmOrderByOrderId(orderId);
    }
}
